package com.zh.harmony.startup.sample;

import com.zh.harmony.startup.AppInitializer;
import com.zh.harmony.startup.Initializer;
import com.zh.harmony.startup.sample.util.LogUtil;
import ohos.aafwk.ability.AbilityPackage;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

public class MyApplication extends AbilityPackage {
    @Override
    public void onInitialize() {
        super.onInitialize();
        //初始化
        ExampleLogger exampleLogger = AppInitializer.getInstance(this).initializeComponent(ExampleLoggerInitializer.class);
        exampleLogger.setTag("StartupHarmony");
        if (exampleLogger.getWorkManager() != null) {
            exampleLogger.info("WorkManager => 依赖存在");
            exampleLogger.info("ExampleLogger => 初始化完成");
        } else {
            exampleLogger.info("ExampleLogger => 初始化失败");
        }
    }

    /**
     * Logger类，依赖WorkManager
     */
    private static class ExampleLogger {
        private final WorkManager workManager;
        private String tag;

        public ExampleLogger(WorkManager workManager) {
            this.workManager = workManager;
        }

        public void setTag(String tag) {
            this.tag = tag;
        }

        public void info(String message) {
            LogUtil.info(tag, message);
        }

        public WorkManager getWorkManager() {
            return workManager;
        }
    }

    /**
     * WorkManager，依赖Context
     */
    private static class WorkManager {
        private static WorkManager sInstance;
        private final Context context;

        public WorkManager(Context context) {
            this.context = context;
        }

        public static WorkManager getInstance(Context context) {
            if (sInstance == null) {
                synchronized (WorkManager.class) {
                    if (sInstance == null) {
                        sInstance = new WorkManager(context);
                    }
                }
            }
            return sInstance;
        }

        public Context getContext() {
            return context;
        }
    }

    /**
     * WorkManager初始化器
     */
    public static class WorkManagerInitializer implements Initializer<WorkManager> {
        @Override
        public WorkManager create(Context context) {
            return WorkManager.getInstance(context);
        }

        @Override
        public List<Class<? extends Initializer<?>>> dependencies() {
            return new ArrayList<>();
        }
    }

    /**
     * ExampleLogger初始化器，依赖WorkManagerInitializer
     */
    public static class ExampleLoggerInitializer implements Initializer<ExampleLogger> {
        @Override
        public ExampleLogger create(Context context) {
            return new ExampleLogger(WorkManager.getInstance(context));
        }

        @Override
        public List<Class<? extends Initializer<?>>> dependencies() {
            ArrayList<Class<? extends Initializer<?>>> dependencies = new ArrayList<>();
            dependencies.add(WorkManagerInitializer.class);
            return dependencies;
        }
    }
}